from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.database.database import engine
from app.models import models
from app.routers import wineries, vineyards, harvests, preprocessings, fermentations, storages, bottlings, quality_checks, orders, logistics

# 创建数据库表
models.Base.metadata.create_all(bind=engine)

app = FastAPI(
    title="葡萄酒溯源系统API",
    description="葡萄酒溯源系统的后端API服务",
    version="1.0.0"
)

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:8080", "http://127.0.0.1:8080"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 包含路由
app.include_router(wineries.router)
app.include_router(vineyards.router)
app.include_router(harvests.router)
app.include_router(preprocessings.router)
app.include_router(fermentations.router)
app.include_router(storages.router)
app.include_router(bottlings.router)
app.include_router(quality_checks.router)
app.include_router(orders.router)
app.include_router(logistics.router)

@app.get("/")
def read_root():
    return {"message": "葡萄酒溯源系统API服务正在运行"}

@app.get("/health")
def health_check():
    return {"status": "healthy"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)
